Right-Shift (>>)
The right shift operator is denoted by the double right arrow key (>>). The general syntax for the right shift is “shift-expression >> k”. The right-shift operator causes the bits in shift expression to be shifted to the right by the number of positions specified by k. For unsigned numbers, the bit positions that the shift operation has vacated are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.
Note: Every time we shift a number towards the right by 1 bit it divides that number by 2.
Example:
Input: Right shift of 5 by 1.
Binary representation of 5 = 00101 and Right shift of 00101 by 1 (i.e, 00101 >> 1)Output: 2
Explanation: All bit of 5 will be shifted by 1 to Rightside and this result in 00010 Which is equivalent to 2Input: Right shift of 5 by 2.
Binary representation of 5 = 00101 and Right shift of 00101 by 2 (i.e, 00101 >> 2)Output: 1
Explanation: All bit of 5 will be shifted by 2 to Right side and this result in 00001, Which is equivalent to 1Input: Right shift of 5 by 3.
Binary representation of 5 = 00101 and Right shift of 00101 by 3 (i.e, 00101 >> 3)Output: 0
Explanation: All bit of 5 will be shifted by 3 to Right side and this result in 00000, Which is equivalent to 0
Implementation of Right shift operator:
#include <bitset>
#include <iostream>
using namespace std;
int main()
{
unsigned int num1 = 1024;
bitset<32> bt1(num1);
cout << bt1 << endl;
unsigned int num2 = num1 >> 1;
bitset<32> bt2(num2);
cout << bt2 << endl;
unsigned int num3 = num1 >> 2;
bitset<16> bitset13{ num3 };
cout << bitset13 << endl;
}
// JavaScript code for the above approach
let num1 = 1024;
let bt1 = num1.toString(2).padStart(32, '0');
console.log(bt1);
let num2 = num1 >> 1;
let bt2 = num2.toString(2).padStart(32, '0');
console.log(bt2);
let num3 = num1 >> 2;
let bitset13 = num3.toString(2).padStart(16, '0');
console.log(bitset13);
// akashish__
Output
00000000000000000000010000000000 00000000000000000000001000000000 0000000100000000
Time Complexity: O(1)
Auxiliary Space: O(1)
Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.
Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.
Table of Content
- What is Bitwise Algorithms?
- Bitwise Operators / Basics of Bit manipulation
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise NOT Operator (!~)
- Left-Shift (<<)
- Right-Shift (>>)
- Application of Bit Operators
- Important Practice Problems on Bitwise Algorithm
Contact Us